    //
//  AnswerViewController.m
//  BetsAndBrains
//
//  Created by Augustine, Eric M (augus011) on 4/9/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "AnswerViewController.h"
#import "BetViewController.h"
#import "Game.h"
#import <UIKit/UIKit.h>
#import "Player.h"
#import "ComputerPlayer.h"
#import "Question.h"
#import "Guess.h"
#import "Bet.h"

@implementation AnswerViewController

@synthesize betView;
@synthesize answer;
@synthesize question;
@synthesize game;

-(IBAction)submitAnswer{
	if(answer.text != @""){
		NSError* error = NULL;
		NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9]+$" 
																			   options:NSRegularExpressionCaseInsensitive 
																				  error:&error];
		NSInteger matches = [regex numberOfMatchesInString:answer.text 
												   options:NSMatchingWithoutAnchoringBounds 
													 range:NSMakeRange(0,answer.text.length)];
		if(matches > 0){
			Player* p = [self.game.players objectAtIndex:0];
			int guess = [answer.text intValue];
			[p submitGuess:guess];
			for(NSInteger i = 1; i < self.game.players.count; i++){
				ComputerPlayer* comp = [self.game.players objectAtIndex:i];
				[comp submitGuess:self.game.currentQuestion.answer];
			}
			betView.game = self.game;
			[self presentModalViewController:betView animated: TRUE];
		}else{
			//Some sort of dialog here
		}
	}else{
		//Some sort of dialog here
	}
}

-(IBAction)options{
	
}

-(IBAction) dismissKeyboard: (id) sender{
	[sender resignFirstResponder];
}

-(IBAction) tapBackground: (id) sender{
	[answer resignFirstResponder];
}

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self.question setText:game.currentQuestion.text];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
	
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.betView = nil;
	self.answer = nil;
	self.question = nil;
	self.game = nil;
}


- (void)dealloc {
    [super dealloc];
	[betView autorelease];
	[answer autorelease];
	[question autorelease];
	[game autorelease];
}


@end
